package novel.impl.chapter;

import novel.entity.ChapterDetail;
import novel.enums.NovelSpiderEnum;
import novel.impl.AbstractCrawlSpider;
import novel.intrerfaces.IchapterDetailSpider;
import novel.util.NovelSpiderUtil;
import org.jsoup.Jsoup;
import org.jsoup.helper.StringUtil;
import org.jsoup.nodes.Document;

import java.util.Map;

/**
 * @Author:xdw
 * @Date:Created in 下午 1:372019/3/11 0011
 * @described:
 * @Modified By:
 */
public abstract class AbstractChapterDetailSpider extends AbstractCrawlSpider implements IchapterDetailSpider {
    @Override
    public ChapterDetail getChapterDetail(String url) {

        try {
            String result = super.crawl(url);
            //解析时将<br/>替换成${n}
            result = result.replace("&nbsp;", " ")
                    .replace("<br />", "${n}")
                    .replace("<br/>", "${n}");
            Document doc = Jsoup.parse(result);
            Map<String, String> context = NovelSpiderUtil.getContext(NovelSpiderEnum.getEnumByUrl(url));
            //获取标题解析规则
            String titleSelector = context.get("chapter-detail-title-selector");
            String[] titleSpites = titleSelector.split("\\,");
            titleSpites = parseSelect(titleSpites);

            ChapterDetail chapterDetail = new ChapterDetail();
            //通过选择器获取标题
            chapterDetail.setTitle(doc.select(titleSpites[0]).get(Integer.parseInt(titleSpites[1])).text());
            //获取章节内容选择器
            String contentSelector = context.get("chapter-detail-content-selector");
            String[] contentSpites = contentSelector.split("\\,");
            contentSpites = parseSelect(contentSpites);
            //获取章节内容 将${n} 替换成\n
            chapterDetail.setContent(doc.select(contentSpites[0]).
                    get(Integer.parseInt(contentSpites[1])).text().replace("${n}","\n"));

            //获取上一张选择器
            String prevSelector=context.get("chapter-detail-prev-selector");
            String[] prevSpites = prevSelector.split("\\,");
            prevSpites = parseSelect(prevSpites);
            //获取上一张
            String href = StringUtil.resolve(url,doc.select(prevSpites[0]).get(Integer.parseInt(prevSpites[1])).attr("href"));
            chapterDetail.setPrev(href);

            //获取下一章
            String nextSelector=context.get("chapter-detail-next-selector");
            String[] nextSpites = nextSelector.split("\\,");
            nextSpites = parseSelect(nextSpites);
            //获取下一张
            String nextHref = StringUtil.resolve(url,doc.select(nextSpites[0]).get(Integer.parseInt(nextSpites[1])).attr("href"));
            chapterDetail.setNext(nextHref);
            return chapterDetail;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    //截取
    private String[] parseSelect(String[] spites) {
        String[] newSpites = new String[2];
        if (spites.length == 1) {
            newSpites[0] = spites[0];
            newSpites[1] = "0";
        }else {
            newSpites=spites;
        }
        return newSpites;
    }
}
